package com.hutao.manage.service.impl;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.comparator.MedicalRecordPrinterImageComparator;
import com.hutao.manage.component.Config;
import com.hutao.manage.component.MyConfig;
import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.BaseInfoMapper;
import com.hutao.manage.mapper.BasicInfoMapper;
import com.hutao.manage.mapper.CitizensInfoMapper;
import com.hutao.manage.mapper.FirstLevelMedicalMapper;
import com.hutao.manage.mapper.HospNoteBasicJsonMapper;
import com.hutao.manage.mapper.HospNoteVersionMapper;
import com.hutao.manage.mapper.HospitalizationNoteMapper;
import com.hutao.manage.mapper.MedicalPrinterRangeMapper;
import com.hutao.manage.mapper.MedicalResourceExaminationMapper;
import com.hutao.manage.mapper.MedicalResourceMapper;
import com.hutao.manage.mapper.MedicalResourcePicMapper;
import com.hutao.manage.mapper.MedicalVisitWarnDetailMapper;
import com.hutao.manage.mapper.MrPageTypeMapper;
import com.hutao.manage.mapper.ScanFileMapper;
import com.hutao.manage.mapper.TreatmentTraceMapper;
import com.hutao.manage.model.HospitalVisitBasicInfo;
import com.hutao.manage.model.HospitalVisitExaminationReportsInfo;
import com.hutao.manage.pojo.CitizensInfo;
import com.hutao.manage.pojo.FirstLevelMedical;
import com.hutao.manage.pojo.HospNoteVersion;
import com.hutao.manage.pojo.HospitalizationNote;
import com.hutao.manage.pojo.MedicalResource;
import com.hutao.manage.pojo.MedicalResourceExamination;
import com.hutao.manage.pojo.MedicalResourcePic;
import com.hutao.manage.pojo.MedicalVisitWarnDetail;
import com.hutao.manage.pojo.MrPageType;
import com.hutao.manage.pojo.ScanFile;
import com.hutao.manage.pojo.TreatmentTrace;
import com.hutao.manage.pojo.cust.HospitalizationNoteBasic;
import com.hutao.manage.pojo.cust.MedicalRecordPrinterImage;
import com.hutao.manage.query.MedRecordQuery;
import com.hutao.manage.query.MedTraceQuery;
import com.hutao.manage.query.MedVisitUrgeQuery;
import com.hutao.manage.query.MedicalSearchQuery;
import com.hutao.manage.service.MedRecordService;
import com.hutao.manage.util.DateUtil;
import com.hutao.manage.util.FileUtil;
import com.hutao.manage.util.JsonUtil;
import com.hutao.manage.util.JsonUtils;
import com.hutao.manage.util.RateUtil;
import com.hutao.manage.util.Result;
import com.hutao.manage.util.StringUtil;
import com.hutao.manage.util.disk.DiskManageUtil;

@Service
public class MedRecordServiceImpl implements MedRecordService {
	private static final Logger logger = LoggerFactory.getLogger(MedRecordServiceImpl.class);

	@Autowired
	private BasicInfoMapper basicInfoMapper;
	@Autowired
    private	HospNoteVersionMapper hospNoteVersionMapper;
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Autowired
	private HospitalizationNoteMapper hospitalizationNoteMapper;
	
	@Autowired
	private ScanFileMapper scanFileMapper;
	
	@Autowired
	private MedicalResourcePicMapper medicalResourcePicMapper;
	
	@Autowired
	private MedicalResourceMapper medicalResourceMapper;
	
	@Autowired
	private FirstLevelMedicalMapper firstLevelMedicalMapper;
	
	@Autowired
	private CitizensInfoMapper citizensInfoMapper;
	
	@Autowired
	private TreatmentTraceMapper treatmentTraceMapper;
	
	@Autowired
	private MedicalPrinterRangeMapper medicalPrinterRangeMapper;
	@Autowired
	private MedicalResourceExaminationMapper medicalResourceExaminationMapper;

	@Autowired
	private MrPageTypeMapper mrPageTypeMapper;
	
	
	@Autowired
	private HospNoteBasicJsonMapper hospNoteBasicJsonMapper;
	

	@Autowired  
    private Config conf; 
	
	@Autowired
	private MyConfig myConfig;
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalizationNoteList(
			MedRecordQuery params) {
		 List<Map<String,Object>> list = hospitalizationNoteMapper.getHospitalizationNoteList(params);
		 
		 for (Map<String, Object> map : list) {
			String firstGuid = (String)map.get("first_level_medical_guid");
			List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PAGINATION);
			int flagCount = treatmentTraceMapper.isPagenationTreatementTrace(firstGuid, (Integer)treatmentTyps.get(0).get("id"));
			String operator = treatmentTraceMapper.selectOperatorByGuidAndTreateCode(firstGuid, TreatmentTrace.VISIT_SCAN);
			map.put("isPage", flagCount>0?2:1);
			map.put("operator", operator==null?"":operator);
		 }
		 
		return hospitalizationNoteMapper.getHospitalizationNoteList(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalizationNoteByFirstGuid(
			String firstGuid) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getHospitalizationNoteByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getScanFiles(String firstGuid) {
		
		return scanFileMapper.getFileMsgListByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getHospitalizationNoteCount(MedRecordQuery params) {
		return hospitalizationNoteMapper.getHospitalizationNoteCount(params);
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int removeMedRecordImage(String firstGuid, String fileHash) {
		int result = 0;
		try{
			MedicalResourcePic medicalResourcePic = medicalResourcePicMapper.selectByFirstGuidAndFileHash(firstGuid, fileHash);
		    result += medicalResourcePicMapper.deleteByPrimaryKey(medicalResourcePic.getId());
		    result += medicalResourceMapper.deleteByPrimaryKey(medicalResourcePic.getMedicalResourceId());
		}catch(Exception ex){
			result = 0;
			ex.printStackTrace();
		}
		
		return result;
	}

	/**
	 * 病案编页
	 */
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int paginationMedrecordImage(String firstGuid, String fileHash,
			Integer mrPageTypeId) {

		MedicalResource medicalResource = medicalResourceMapper.selectByFirstGuidAndFileHash(fileHash, firstGuid);
		
		if(medicalResource !=null){
			medicalResource.setMrPageType(mrPageTypeId);
			medicalResource.setCreateDate(new Date());
			medicalResource.setStatus(1);
			medicalResourceMapper.updateByPrimaryKey(medicalResource);
			/*List<Integer> printPageTypeList = medicalPrinterRangeMapper.getPrintPageByPrintType(1);
			if(printPageTypeList.contains(mrPageTypeId)){
				Map<String,Object> scanFile = scanFileMapper.selectScanFileByFileHash(fileHash);
				String fileName = (String) scanFile.get("fileName");
				String fileUrl = (String) scanFile.get("fileUrl");
				
				FileUtil.createFloder(conf.getUploadResourcePath()+"/printMarkImg");
				File markPicTempFile = new File(conf.getUploadResourcePath()+"printMarkImg/"+fileName);
				if(!markPicTempFile.exists()){
					int width= GraphicsMagicUtil.getWidth(conf.getUploadResourcePath()+fileUrl+fileName);
					int height=GraphicsMagicUtil.getHeight(conf.getUploadResourcePath()+fileUrl+fileName);
					int startX=(width-500)/2;
					int startY=(height-500)/2;
					try {
						GraphicsMagicUtil.addWatermarkToImg(conf.getUploadResourcePath()+fileUrl+fileName,conf.getUploadResourcePath()+"printMarkImg/"+fileName, conf.getWaterMarkLogo(), 500, 500,
								startX, startY, 20);
					} catch (Exception e) {
						
						e.printStackTrace();
					}
				}
			}*/
			return 1;
		}
		return 0;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int updateMedResource(String firstGuid, String fileHash,
			Integer status) {
		MedicalResource medicalResource = medicalResourceMapper.selectByFirstGuidAndFileHash(fileHash, firstGuid);
		if(medicalResource !=null){
			medicalResource.setCreateDate(new Date());
			medicalResource.setStatus(status);
			return medicalResourceMapper.updateByPrimaryKey(medicalResource);
		}
		return 0;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public boolean medicalResourceIsExist(String firstGuid, String fileHash){
		MedicalResource medicalResource = medicalResourceMapper.selectByFirstGuidAndFileHash(fileHash, firstGuid);
		if(medicalResource!=null){
			return true;
		}else{
			return false;
		}
	}

	/**
     * 依据就诊Guid获取住院基本信息
     * @param firstGuid
     * @return
     */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalizationNoteBasicByFirstGuid(
			String firstGuid) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getHospitalizationNoteBasicByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int updateHospitalizationNoteBasic(HospitalizationNoteBasic noteBasic) {
		// TODO Auto-generated method stub
		FirstLevelMedical firstLevelMedical = firstLevelMedicalMapper.selectByPrimaryKey(noteBasic.getFirstGuid());
		noteBasic.synchronizationFirstLevelMedical(firstLevelMedical);
		CitizensInfo citizensInfo = citizensInfoMapper.selectByPrimaryKey(firstLevelMedical.getCitizensGuid());
		noteBasic.synchronizationCitizensInfo(citizensInfo);
		citizensInfoMapper.updateByPrimaryKey(citizensInfo);
		HospitalizationNote hospitalizationNote = hospitalizationNoteMapper.selectByFirstGuid(noteBasic.getFirstGuid());
		noteBasic.synchronizationHospitalizationNote(hospitalizationNote);
		hospitalizationNoteMapper.updateByPrimaryKey(hospitalizationNote);
		return firstLevelMedicalMapper.updateByPrimaryKey(firstLevelMedical);
		
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<MedicalRecordPrinterImage> getMedicalRecordPrinterImages(String firstGuid,
			Integer printerTypeId) {
		// TODO Auto-generated method stub
		
		List<Map<String, Object>> scanFiles = scanFileMapper.getPaginationScanFilesByFirstGuidAndPrinterTypeId(firstGuid, printerTypeId);
		List<MedicalRecordPrinterImage> printerImages = new ArrayList<MedicalRecordPrinterImage>();
		MedicalRecordPrinterImage printerImage =null;
		
		for (Map<String, Object> scanFile : scanFiles) {
			
			printerImage = new MedicalRecordPrinterImage();
			printerImage.setPageTypeName((String)scanFile.get("pageTypeName"));
			printerImage.setPrinterTypeId((Integer)scanFile.get("sortCode"));
			String fileName = (String)scanFile.get("fileName");
			printerImage.setFileName(fileName);
			
			if(scanFile.get("createDate")!=null){
				printerImage.setCreateDate((String)scanFile.get("createDate"));
				
			}
			
			if(scanFile.get("original_file_name")!=null){
				String originaFileName = (String)scanFile.get("original_file_name");
				int length = originaFileName.indexOf(".");
				String originaName = originaFileName.substring(0, length);
				printerImage.setOriginalName(originaName);
			}
			
			String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
					(String)scanFile.get("fileUrl"), fileName);
			
			printerImage.setFileUrl((String)scanFile.get("fileUrl"));
			
			//获取图片真正的位置
			printerImage.setHostUrl(rootPicRealUrl);
			printerImage.setImageUrl(rootPicRealUrl + (String)scanFile.get("fileUrl") + (String)scanFile.get("fileName"));
			printerImages.add(printerImage);
		}
		
		List<Integer> printTypeList = medicalPrinterRangeMapper.getPrintPageByPrintType(printerTypeId);
		int examinationFileIndex = 1000000;
		
		if(printTypeList.contains(29)){
			MrPageType examinationMrPageType = mrPageTypeMapper.selectByPrimaryKey(29);
			List<MedicalResourceExamination> medicalResourceExaminationList = medicalResourceExaminationMapper.getMedicalExaminationPicByGuid(firstGuid);
			
			for (MedicalResourceExamination medicalResourceExamination : medicalResourceExaminationList) {
				//得到真实的url根路径
				String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
						"examination/"+firstGuid+"/", medicalResourceExamination.getFileName());
				
				printerImage = new MedicalRecordPrinterImage();
				printerImage.setPrinterTypeId(examinationMrPageType.getSortCode());
				printerImage.setPageTypeName(examinationMrPageType.getName());
				printerImage.setFileName(medicalResourceExamination.getFileName());
				printerImage.setFileUrl("examination/"+firstGuid+"/");
				printerImage.setHostUrl(rootPicRealUrl);
				printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
				printerImage.setOriginalName(""+examinationFileIndex+medicalResourceExamination.getId());
				printerImage.setImageUrl(rootPicRealUrl+"examination/"+firstGuid+"/"+medicalResourceExamination.getFileName());
				printerImages.add(printerImage);
			}
		}
		
		//[start] 获取从远卓护理接口获取的数据
		boolean nurseResult = false;
		if(nurseResult){
			List<String> hospNoteJsonList=hospNoteBasicJsonMapper.getHospBasicJsonByFirstGuid(StringUtil.handleJsonParam(firstGuid));
			Map<String, Object> jsonDataMap = JsonUtils.jsonToPojo(hospNoteJsonList.get(0), Map.class);
			Object recordSheetObj = jsonDataMap.get("recordSheet");
			if(recordSheetObj != null){
				Map<String, Object> recordSheetMap = (Map<String, Object>)recordSheetObj;
				
				//[start] 获取从远卓护理接口获取的体温单数据
				if(printTypeList.contains(46)){
					Object temperatureRecordObj = recordSheetMap.get("temperatureRecord");
					if(temperatureRecordObj!= null){
						Map<String, Object> temperatureRecordMap = (Map<String, Object>)temperatureRecordObj;
						Object imageFilePathListObj = temperatureRecordMap.get("imageFilePathList");
						if(imageFilePathListObj!= null){
							MrPageType temperatureMrPageType = mrPageTypeMapper.selectByPrimaryKey(46);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(temperatureMrPageType.getSortCode());
								printerImage.setPageTypeName(temperatureMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/tiwen/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/tiwen/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
							}
						}
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的血糖记录单数据
				if(printTypeList.contains(30)){
					Object bloodSugarRecordObj = recordSheetMap.get("bloodSugarRecord");
					if(bloodSugarRecordObj!= null){
						Map<String, Object> bloodSugarRecordMap = (Map<String, Object>)bloodSugarRecordObj;
						Object imageFilePathListObj = bloodSugarRecordMap.get("imageFilePathList");
						if(imageFilePathListObj != null){
							MrPageType bloodSugarMrPageType = mrPageTypeMapper.selectByPrimaryKey(30);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(bloodSugarMrPageType.getSortCode());
								printerImage.setPageTypeName(bloodSugarMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/xuetcd/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/xuetcd/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
							}
						}
						
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的心电监测数据
				if(printTypeList.contains(38)){
					Object electrocardiogramRecordObj = recordSheetMap.get("electrocardiogramRecord");
					if(electrocardiogramRecordObj!= null){
						Map<String, Object> electrocardiogramRecordMap = (Map<String, Object>)electrocardiogramRecordObj;
						Object imageFilePathListObj = electrocardiogramRecordMap.get("imageFilePathList");
						if(imageFilePathListObj != null){
							MrPageType electrocardiogramRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(38);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(electrocardiogramRecordMrPageType.getSortCode());
								printerImage.setPageTypeName(electrocardiogramRecordMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/xindjc/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/xindjc/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
							}
						}
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的生命体征观察数据
				if(printTypeList.contains(48)){
					Object vitalSignRecordObj = recordSheetMap.get("vitalSignRecord");
					if(vitalSignRecordObj!= null){
						Map<String, Object> vitalSignRecordMap = (Map<String, Object>)vitalSignRecordObj;
						Object imageFilePathListObj = vitalSignRecordMap.get("imageFilePathList");
						if(imageFilePathListObj!= null){
							MrPageType vitalSignMrPageType = mrPageTypeMapper.selectByPrimaryKey(48);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(vitalSignMrPageType.getSortCode());
								printerImage.setPageTypeName(vitalSignMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/smtzgc/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/smtzgc/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
							}
						}
					}
				}
				
				//[end]
				
				
				//[start] 获取从远卓护理接口获取的护理记录数据
				Object nurseRecordObj = recordSheetMap.get("nurseRecord");
				if(nurseRecordObj!= null){
					Map<String, Object> nurseRecordMap = (Map<String, Object>)nurseRecordObj;
					
					//[start] 获取从远卓护理接口获取的病重护理记录数据
					if(printTypeList.contains(57)){
						Object IllNurseRecordObj = nurseRecordMap.get("IllNurseRecord");
						if(IllNurseRecordObj != null){
							Map<String, Object> IllNurseRecordMap = (Map<String, Object>)IllNurseRecordObj;
							Object imageFilePathListObj = IllNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType IllNurseMrPageType = mrPageTypeMapper.selectByPrimaryKey(57);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(IllNurseMrPageType.getSortCode());
									printerImage.setPageTypeName(IllNurseMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/bingzhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/bingzhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
								}
							}
						}
					}
					
					//[end]
					
					//[start] 获取从远卓护理接口获取的特别护理记录数据
					if(printTypeList.contains(49)){
						Object specialNurseRecordObj = nurseRecordMap.get("specialNurseRecord");
						if(specialNurseRecordObj != null){
							Map<String, Object> specialNurseRecordMap = (Map<String, Object>)specialNurseRecordObj;
							Object imageFilePathListObj = specialNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType specialNurseRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(49);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(specialNurseRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(specialNurseRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/tebiehj/"+firstGuid+"/", imageFilePath);
									printerImage.setFileUrl("CMIS/tebiehj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
								}
							}
						}
					}
					
					//[end]
					
					//[start] 获取从远卓护理接口获取的病危护理记录数据
					if(printTypeList.contains(57)){
						Object criticalCareRecordObj = nurseRecordMap.get("criticalCareRecord");
						if(criticalCareRecordObj != null){
							Map<String, Object> criticalCareRecordMap = (Map<String, Object>)criticalCareRecordObj;
							Object imageFilePathListObj = criticalCareRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType criticalCareRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(57);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(criticalCareRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(criticalCareRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/bingwhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/bingwhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
								}
							}
						}
					}
					
					//[end]
					
					
					//[start] 获取从远卓护理接口获取的一般护理记录数据
					if(printTypeList.contains(59)){
						Object generalNurseRecordObj = nurseRecordMap.get("generalNurseRecord");
						if(generalNurseRecordObj != null){
							Map<String, Object> generalNurseRecordMap = (Map<String, Object>)generalNurseRecordObj;
							Object imageFilePathListObj = generalNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType generalNurseRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(59);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(generalNurseRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(generalNurseRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/yibanhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/yibanhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
								}
							}
						}
					}
					
					//[end]
				}
				//[end]
			
			}
		}
		//[end]
		
		
		return printerImages;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<MedicalRecordPrinterImage> getPreviewMedicalRecordImages(String firstGuid) {
		// TODO Auto-generated method stub
		
		List<Map<String, Object>> scanFiles = scanFileMapper.getPreviewScanFilesByFirstGuid(firstGuid);
		List<MedicalRecordPrinterImage> printerImages = new ArrayList<MedicalRecordPrinterImage>();
		MedicalRecordPrinterImage printerImage =null;
		for (Map<String, Object> scanFile : scanFiles) {
			printerImage = new MedicalRecordPrinterImage();
			printerImage.setPageTypeName((String)scanFile.get("pageTypeName"));
			printerImage.setPrinterTypeId((Integer)scanFile.get("sortCode"));
			String fileName = (String)scanFile.get("fileName");
			printerImage.setFileName((String)scanFile.get("fileName"));
			
			if(scanFile.get("createDate")!=null){
				printerImage.setCreateDate((String)scanFile.get("createDate"));
				
			}
			
			if(scanFile.get("original_file_name")!=null) {
				String originaFileName = (String)scanFile.get("original_file_name");
				int length = originaFileName.indexOf(".");
				String originaName = originaFileName.substring(0, length);
				
				printerImage.setOriginalName(originaName);
			}
			
			String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
					(String)scanFile.get("fileUrl"), fileName);
			
			printerImage.setFileUrl((String)scanFile.get("fileUrl"));
			printerImage.setHostUrl(rootPicRealUrl);
			printerImage.setImageUrl(rootPicRealUrl+(String)scanFile.get("fileUrl")+(String)scanFile.get("fileName"));
			printerImages.add(printerImage);
		}
		
		MrPageType examinationMrPageType = mrPageTypeMapper.selectByPrimaryKey(29);
		List<MedicalResourceExamination> medicalResourceExaminationList = medicalResourceExaminationMapper.getMedicalExaminationPicByGuid(firstGuid);
		
			int examinationFileIndex = 1000000;
			for (MedicalResourceExamination medicalResourceExamination : medicalResourceExaminationList) {
				//得到真实的url根路径
				String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
						"examination/"+firstGuid+"/", medicalResourceExamination.getFileName());
				
				printerImage = new MedicalRecordPrinterImage();
				printerImage.setPrinterTypeId(examinationMrPageType.getSortCode());
				printerImage.setPageTypeName(examinationMrPageType.getName());
				printerImage.setFileName(medicalResourceExamination.getFileName());
				printerImage.setFileUrl("examination/"+firstGuid+"/");
				printerImage.setHostUrl(rootPicRealUrl);
				printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
				printerImage.setOriginalName(""+examinationFileIndex+medicalResourceExamination.getId());
				printerImage.setImageUrl(rootPicRealUrl+"examination/"+firstGuid+"/"+medicalResourceExamination.getFileName());
				printerImages.add(printerImage);
		}
		
		//[start] 获取从远卓护理接口获取的数据
		boolean nurseResult = false;
		if(nurseResult){
			List<String> hospNoteJsonList=hospNoteBasicJsonMapper.getHospBasicJsonByFirstGuid(StringUtil.handleJsonParam(firstGuid));
			Map<String, Object> jsonDataMap = JsonUtils.jsonToPojo(hospNoteJsonList.get(0), Map.class);
			Object recordSheetObj = jsonDataMap.get("recordSheet");
			if(recordSheetObj != null){
				Map<String, Object> recordSheetMap = (Map<String, Object>)recordSheetObj;
				
				//[start] 获取从远卓护理接口获取的体温单数据
					Object temperatureRecordObj = recordSheetMap.get("temperatureRecord");
					if(temperatureRecordObj!= null){
						Map<String, Object> temperatureRecordMap = (Map<String, Object>)temperatureRecordObj;
						Object imageFilePathListObj = temperatureRecordMap.get("imageFilePathList");
						if(imageFilePathListObj!= null){
							MrPageType temperatureMrPageType = mrPageTypeMapper.selectByPrimaryKey(46);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(temperatureMrPageType.getSortCode());
								printerImage.setPageTypeName(temperatureMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/tiwen/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/tiwen/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
						}
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的血糖记录单数据
					Object bloodSugarRecordObj = recordSheetMap.get("bloodSugarRecord");
					if(bloodSugarRecordObj!= null){
						Map<String, Object> bloodSugarRecordMap = (Map<String, Object>)bloodSugarRecordObj;
						Object imageFilePathListObj = bloodSugarRecordMap.get("imageFilePathList");
						if(imageFilePathListObj != null){
							MrPageType bloodSugarMrPageType = mrPageTypeMapper.selectByPrimaryKey(30);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(bloodSugarMrPageType.getSortCode());
								printerImage.setPageTypeName(bloodSugarMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/xuetcd/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/xuetcd/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
						}
						
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的心电监测数据
					Object electrocardiogramRecordObj = recordSheetMap.get("electrocardiogramRecord");
					if(electrocardiogramRecordObj!= null){
						Map<String, Object> electrocardiogramRecordMap = (Map<String, Object>)electrocardiogramRecordObj;
						Object imageFilePathListObj = electrocardiogramRecordMap.get("imageFilePathList");
						if(imageFilePathListObj != null){
							MrPageType electrocardiogramRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(38);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(electrocardiogramRecordMrPageType.getSortCode());
								printerImage.setPageTypeName(electrocardiogramRecordMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/xindjc/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/xindjc/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
							}
					}
				}
				
				//[end]
				
				//[start] 获取从远卓护理接口获取的生命体征观察数据
					Object vitalSignRecordObj = recordSheetMap.get("vitalSignRecord");
					if(vitalSignRecordObj!= null){
						Map<String, Object> vitalSignRecordMap = (Map<String, Object>)vitalSignRecordObj;
						Object imageFilePathListObj = vitalSignRecordMap.get("imageFilePathList");
						if(imageFilePathListObj!= null){
							MrPageType vitalSignMrPageType = mrPageTypeMapper.selectByPrimaryKey(48);
							List<String> imageFilePathList = (List<String>)imageFilePathListObj;
							for (String imageFilePath : imageFilePathList) {
								printerImage = new MedicalRecordPrinterImage();
								printerImage.setPrinterTypeId(vitalSignMrPageType.getSortCode());
								printerImage.setPageTypeName(vitalSignMrPageType.getName());
								printerImage.setFileName(imageFilePath);
								
								String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
										"CMIS/smtzgc/"+firstGuid+"/", imageFilePath);
								
								printerImage.setFileUrl("CMIS/smtzgc/"+firstGuid+"/");
								printerImage.setHostUrl(rootPicRealUrl);
								
								printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
								printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
								printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
								
								if (StringUtils.isNotBlank(rootPicRealUrl)) {
									printerImages.add(printerImage);
								}
								
						}
					}
				}
				
				//[end]
				
				
				//[start] 获取从远卓护理接口获取的护理记录数据
				Object nurseRecordObj = recordSheetMap.get("nurseRecord");
				if(nurseRecordObj!= null){
					Map<String, Object> nurseRecordMap = (Map<String, Object>)nurseRecordObj;
					
					//[start] 获取从远卓护理接口获取的病重护理记录数据
						Object IllNurseRecordObj = nurseRecordMap.get("IllNurseRecord");
						if(IllNurseRecordObj != null){
							Map<String, Object> IllNurseRecordMap = (Map<String, Object>)IllNurseRecordObj;
							Object imageFilePathListObj = IllNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType IllNurseMrPageType = mrPageTypeMapper.selectByPrimaryKey(57);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(IllNurseMrPageType.getSortCode());
									printerImage.setPageTypeName(IllNurseMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/bingzhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/bingzhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
							}
						}
					}
					
					//[end]
					
					//[start] 获取从远卓护理接口获取的特别护理记录数据
						Object specialNurseRecordObj = nurseRecordMap.get("specialNurseRecord");
						if(specialNurseRecordObj != null){
							Map<String, Object> specialNurseRecordMap = (Map<String, Object>)specialNurseRecordObj;
							Object imageFilePathListObj = specialNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType specialNurseRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(49);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(specialNurseRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(specialNurseRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/tebiehj/"+firstGuid+"/", imageFilePath);
									printerImage.setFileUrl("CMIS/tebiehj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
							}
						}
					}
					
					//[end]
					
					//[start] 获取从远卓护理接口获取的病危护理记录数据
						Object criticalCareRecordObj = nurseRecordMap.get("criticalCareRecord");
						if(criticalCareRecordObj != null){
							Map<String, Object> criticalCareRecordMap = (Map<String, Object>)criticalCareRecordObj;
							Object imageFilePathListObj = criticalCareRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType criticalCareRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(57);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(criticalCareRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(criticalCareRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/bingwhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/bingwhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
								}
						}
					}
					
					//[end]
					
					
					//[start] 获取从远卓护理接口获取的一般护理记录数据
						Object generalNurseRecordObj = nurseRecordMap.get("generalNurseRecord");
						if(generalNurseRecordObj != null){
							Map<String, Object> generalNurseRecordMap = (Map<String, Object>)generalNurseRecordObj;
							Object imageFilePathListObj = generalNurseRecordMap.get("imageFilePathList");
							if(imageFilePathListObj!= null){
								MrPageType generalNurseRecordMrPageType = mrPageTypeMapper.selectByPrimaryKey(59);
								List<String> imageFilePathList = (List<String>)imageFilePathListObj;
								for (String imageFilePath : imageFilePathList) {
									printerImage = new MedicalRecordPrinterImage();
									printerImage.setPrinterTypeId(generalNurseRecordMrPageType.getSortCode());
									printerImage.setPageTypeName(generalNurseRecordMrPageType.getName());
									printerImage.setFileName(imageFilePath);
									
									String rootPicRealUrl = DiskManageUtil.getRootPicRealUrl(conf.getConfigFileStorageAllDisk(), conf.getUploadResourcePath(), conf.getHospitalizationNoteResourceUrl(), 
											"CMIS/yibanhj/"+firstGuid+"/", imageFilePath);
									
									printerImage.setFileUrl("CMIS/yibanhj/"+firstGuid+"/");
									printerImage.setHostUrl(rootPicRealUrl);
									
									printerImage.setCreateDate(DateUtil.dateFormat("yyyy-MM-dd HH:mm:ss", new Date()));
									printerImage.setOriginalName(imageFilePath.substring(0, imageFilePath.lastIndexOf(".")));
									printerImage.setImageUrl(rootPicRealUrl+printerImage.getFileUrl()+printerImage.getFileName());
									
									if (StringUtils.isNotBlank(rootPicRealUrl)) {
										printerImages.add(printerImage);
									}
									
								}
							}
						}
					}
					
					//[end]
				}
				//[end]
			
		}
		//[end]
		Collections.sort(printerImages, new MedicalRecordPrinterImageComparator());
		return printerImages;
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void addTreatmentTrace(String firstGuid, String userId,
			Integer traceTypeId) {

		TreatmentTrace treatmentTrace = new TreatmentTrace();
		treatmentTrace.setUserId(userId);
		treatmentTrace.setTraceTypeId(traceTypeId);
		treatmentTrace.setTreatmentGuid(firstGuid);
		treatmentTraceMapper.insert(treatmentTrace);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalizationNoteOfPrinterList(
			MedRecordQuery params) {
		return hospitalizationNoteMapper.getHospitalizationNoteOfPrinterList(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getHospitalizationNoteCountOfPrinter(MedRecordQuery params) {
		return hospitalizationNoteMapper.getHospitalizationNoteCountOfPrinter(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> statisticsMedRecordOfPigeonhole(MedTraceQuery medTraceQuery,int beyondDay) {
		Map<String, Object> collectionStatistics = new HashMap<String, Object>();
		// TODO Auto-generated method stub
		List<Map<String, Object>> collectionOfMedRecords = hospitalizationNoteMapper.getCollectionStatistics(medTraceQuery);
		List<Map<String, Object>> showCollectionOfMedRecords = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> hospitalHolidays = new ArrayList<Map<String,Object>>();
		int dealType = 0;
		if(medTraceQuery.getOutHospitalStartDate() != null && medTraceQuery.getOutHospitalEndDate()!=null 
				&& medTraceQuery.getTraceStartDate() != null && medTraceQuery.getTraceEndDate() !=null){
			dealType = 1;
		}else if(medTraceQuery.getTraceStartDate() != null && medTraceQuery.getTraceEndDate() !=null){
			dealType = 1;
		}else if(medTraceQuery.getOutHospitalStartDate() != null && medTraceQuery.getOutHospitalEndDate()!=null){
			dealType = 0;
		}else{
			dealType = 0;
		}
		
		if(dealType ==0){
			Date minDate = null;
			Date maxDate = null;
			for (Map<String, Object> collectionOfMedRecord : collectionOfMedRecords) {
				
				Date outHospitalDate = (Date)collectionOfMedRecord.get("out_hospital_date");
				Date collectionDate = (Date)collectionOfMedRecord.get("trace_date");
				if(outHospitalDate.compareTo(collectionDate) >=0){
					if(minDate== null){
						minDate = collectionDate;
					}else{
						if(minDate.compareTo(collectionDate) >=0){
							minDate = collectionDate;
						}
					}
					if(maxDate == null){
						maxDate = outHospitalDate;
					}else{
						if(maxDate.compareTo(outHospitalDate)<0){
							maxDate = outHospitalDate;
						}
					}
				}else{
					if(minDate== null){
						minDate = outHospitalDate;
					}else{
						if(minDate.compareTo(outHospitalDate) >=0){
							minDate = outHospitalDate;
						}
					}
					if(maxDate == null){
						maxDate = collectionDate;
					}else{
						if(maxDate.compareTo(collectionDate)<0){
							maxDate = collectionDate;
						}
					}
				}
			}
			if(minDate != null && maxDate != null){
				hospitalHolidays = basicInfoMapper.getHospitalHolidaysByStartDateAndEndDate(minDate, maxDate);
			}
			for (Map<String, Object> collectionOfMedRecord : collectionOfMedRecords) {
				
				Date outHospitalDate = (Date)collectionOfMedRecord.get("out_hospital_date");
				Date collectionDate = (Date)collectionOfMedRecord.get("trace_date");
				int diffDays = 0;
				try {
					diffDays = DateUtil.getDaysBetween(collectionDate, outHospitalDate)-1;
					for (Map<String, Object> hospitalHoliday : hospitalHolidays) {
						Date hospitalHolidayDate = (Date)hospitalHoliday.get("holidays_date");
						if(DateUtil.compare(outHospitalDate, hospitalHolidayDate) <=0 && DateUtil.compare(hospitalHolidayDate,collectionDate) <=0){
							diffDays = diffDays - 1;
						}
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				collectionOfMedRecord.put("date_diff", String.valueOf(diffDays));
				if(diffDays > beyondDay){
					showCollectionOfMedRecords.add(collectionOfMedRecord);
				}
			}
		}else{
			hospitalHolidays = basicInfoMapper.getHospitalHolidaysByStartDateAndEndDate(medTraceQuery.getTraceStartDate(),medTraceQuery.getTraceEndDate());
			for (Map<String, Object> collectionOfMedRecord : collectionOfMedRecords) {
				
				Date outHospitalDate = (Date)collectionOfMedRecord.get("out_hospital_date");
				Date collectionDate = (Date)collectionOfMedRecord.get("trace_date");
				int diffDays = 0;
				try {
					diffDays = DateUtil.getDaysBetween(collectionDate, outHospitalDate)-1;
					for (Map<String, Object> hospitalHoliday : hospitalHolidays) {
						Date hospitalHolidayDate = (Date)hospitalHoliday.get("holidays_date");
						if(DateUtil.compare(outHospitalDate, hospitalHolidayDate) <=0 && DateUtil.compare(hospitalHolidayDate,collectionDate) <=0){
							diffDays = diffDays - 1;
						}
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				collectionOfMedRecord.put("date_diff", String.valueOf(diffDays));
				if(diffDays > beyondDay){
					showCollectionOfMedRecords.add(collectionOfMedRecord);
				}
			}
		}
		
		
		Map<String, Object> collectResult = new HashMap<String, Object>();
		float collectRate = 0;
		if(collectionOfMedRecords.size() > 0){
			collectRate = RateUtil.getPercent(collectionOfMedRecords.size() - showCollectionOfMedRecords.size(), collectionOfMedRecords.size());
			collectResult.put("collectTotal", collectionOfMedRecords.size());
			collectResult.put("collectRate", collectRate);
			
		}
		collectionStatistics.put("collectResult", collectResult);
		collectionStatistics.put("detail", showCollectionOfMedRecords);
		
		return collectionStatistics;
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public Result addSimpleMedRecord(HospitalizationNoteBasic hospitalNote,String currentUser) {
		//判断病案是否已经存在
		List<Map<String, Object>> hospitalizationNoteList = hospitalizationNoteMapper.
				getHospNoteByMrIdAndVisitNumber(hospitalNote.getMrId(), hospitalNote.getInHospitalNumber()+"");
		
		if (!CollectionUtils.isEmpty(hospitalizationNoteList)) {
			return new Result(false, "ID号=" + hospitalNote.getMrId() + " 住院次数=" + hospitalNote.getInHospitalNumber() + " 已经存在了");
		}
		
		// TODO Auto-generated method stub
		CitizensInfo citizensInfo = new CitizensInfo();
		String guid=StringUtil.getId();
		citizensInfo.setGuid(guid);
		citizensInfo.setName(hospitalNote.getName());
		citizensInfo.setIdType(hospitalNote.getIdentityDocumentTypeId());
		citizensInfo.setIdNumber(hospitalNote.getIdNumber());
		citizensInfo.setSexId(hospitalNote.getSexId());
		citizensInfo.setBirthday(hospitalNote.getBirthday());
		citizensInfo.setCreateDate(new Date());
		citizensInfo.setStatus(1);
		citizensInfoMapper.insert(citizensInfo);
		
		FirstLevelMedical firstLevelMedical = new FirstLevelMedical();
		String firstLevelGuid = StringUtil.getId();
		firstLevelMedical.setGuid(firstLevelGuid);
		firstLevelMedical.setHospitalId(1);
		firstLevelMedical.setCreateDate(new Date());
		firstLevelMedical.setCitizensGuid(guid);
		firstLevelMedical.setStatus(1);
		firstLevelMedical.setTreatmentTypeId(1);
		firstLevelMedical.setTreatmentSignId(1);
		firstLevelMedicalMapper.insert(firstLevelMedical);
		
		//病案初始化
		TreatmentTrace treatmentTrace = new TreatmentTrace();
		treatmentTrace.setUserId(currentUser);
		List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_INIT);
		treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
		treatmentTrace.setTreatmentGuid(firstLevelMedical.getGuid());
		treatmentTraceMapper.insert(treatmentTrace);
		
		//病案归档
		List<Map<String,Object>> pigeonholeTreatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PIGEONHOLE);
		treatmentTrace.setTraceTypeId((Integer)pigeonholeTreatmentTyps.get(0).get("id"));
		treatmentTraceMapper.insert(treatmentTrace);
		
		HospitalizationNote hospitalizationNote = new HospitalizationNote();
		hospitalizationNote.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
		hospitalizationNote.setMrId(hospitalNote.getMrId());
		hospitalizationNote.setOnlyId(hospitalNote.getOnlyId());
		hospitalizationNote.setOutDeptId(hospitalNote.getOutDeptId());
		hospitalizationNote.setOutHospitalTypeId(hospitalNote.getOutHospitalTypeId());
		hospitalizationNote.setOutHospitalDate(hospitalNote.getOutHospitalDate());
		//普通病案
		hospitalizationNote.setSpecialIndication(1);
		
		//入院时间
		hospitalizationNote.setInHospitalDate(hospitalNote.getInHospitalDate());
		//住院次数
		hospitalizationNote.setVisitNumber(hospitalNote.getInHospitalNumber());
		//入院科室
		hospitalizationNote.setInDeptId(hospitalNote.getInDeptId());
		
		hospitalizationNoteMapper.insert(hospitalizationNote);
		
		/*HospitalizationNoteBasic noteBasic = new HospitalizationNoteBasic();
		noteBasic.setName(hospitalNote.getName());*/
		
		//构造json
		Map<String, Object> hospitalVisitFillInfo = new HashMap<String, Object>();
		HospitalVisitBasicInfo basicInfo = new HospitalVisitBasicInfo();
		basicInfo.setVisitGuid(firstLevelGuid);
	
		//患者姓名
		basicInfo.setPatientName(hospitalNote.getName());
		
		//身份证号
		if (hospitalNote.getIdentityDocumentTypeId() != null) {
			basicInfo.setIdDocumentTypeCode(hospitalNote.getIdentityDocumentTypeId()+"");
			basicInfo.setIdDocumentTypeName("身份证号");
		}
		
		basicInfo.setIdNumber(hospitalNote.getIdNumber());
		//性别编号
		basicInfo.setSexCode(hospitalNote.getSexId()+"");
		//性别名称
		if (hospitalNote.getSexId() != null) {
			basicInfo.setSexCode(hospitalNote.getSexId()+"");
			basicInfo.setSexName(hospitalNote.getSexName());
		}
		
		//出生日期
		basicInfo.setBirthday(hospitalNote.getBirthday());
		
		//ID号
		basicInfo.setMrId(hospitalNote.getMrId());
		
		//病案号
		basicInfo.setOnlyId(hospitalNote.getOnlyId());
		
		//入院时间
		basicInfo.setInHospitalDateTime(hospitalNote.getInHospitalDate());
		
		//入院科室编号
		if (hospitalNote.getInDeptId() != null) {
			basicInfo.setInDeptCode(hospitalNote.getInDeptId() + "");
			basicInfo.setInDeptName(hospitalNote.getInDeptName());
		}
		
		//住院次数
		if (null != hospitalNote.getInHospitalNumber()) {
			basicInfo.setVisitNumber(new BigDecimal(hospitalNote.getInHospitalNumber()));
		}
		
		//出院时间
		basicInfo.setOutHospitalDateTime(hospitalNote.getOutHospitalDate());
		
		//出院科室编号
		if (hospitalNote.getOutDeptId() != null) {
			basicInfo.setOutDeptCode(hospitalNote.getOutDeptId() + "");
			basicInfo.setOutDeptName(hospitalNote.getOutDeptName());
		}
		
		//离院方式编号
		if (hospitalNote.getOutHospitalTypeId() != null) {
			basicInfo.setOutHospitalTypeCode(hospitalNote.getOutHospitalTypeId() + "");
			basicInfo.setOutHospitalTypeName(hospitalNote.getOutHospitalTypeName());
		}
		
		hospitalVisitFillInfo.put("firstGuid", basicInfo.getVisitGuid());
		//基本信息
		hospitalVisitFillInfo.put("basicInfo", basicInfo);
		
		String filePath = myConfig.getHospitalVisitJsonPath() + StringUtil.getLocalPath(basicInfo.getVisitGuid());
//		String versionFilePath = myConfig.getHospitalVisitJsonVersionPath()
//				+ StringUtil.getLocalPath(basicInfo.getVisitGuid());
		String versionFilePath = myConfig.getHospitalVisitJsonVersionPath()+basicInfo.getVisitGuid()+"/";
		String fileName = basicInfo.getVisitGuid() + ".json";
		//String versionFileName = basicInfo.getVisitGuid() + "-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date())
		//		+ ".json";
		String versionFileName =DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
		hospitalVisitFillInfo.put("userId", currentUser);
		hospitalVisitFillInfo.put("userName", currentUser);
		
		//检验报告单
		HospitalVisitExaminationReportsInfo examinationReportsInfo = new HospitalVisitExaminationReportsInfo();
		hospitalVisitFillInfo.put("examinationReportsInfo", examinationReportsInfo);
		
		String jsonMapJson = JsonUtil.objectToJson(hospitalVisitFillInfo);
		
		try {
			String diskName = DiskManageUtil.getDiskNameByFileName(conf.getConfigFileStorageAllDisk(), filePath + fileName);
			
			if (StringUtils.isBlank(diskName)) {
				FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			} else {
				//写入到本地json文件
				String realFilePath = diskName + filePath.trim().substring(1);
				FileUtil.createOrEditFile(jsonMapJson, realFilePath, fileName);
			}
			
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
		} catch (IOException e) {
			e.printStackTrace();
		}

		List<String> hbList = hospNoteBasicJsonMapper.getHospBasicJsonByFirstGuid("\"" + basicInfo.getVisitGuid() + "\"");
		if (null == hbList || hbList.size() == 0) {
			hospNoteBasicJsonMapper.insert(jsonMapJson);
		} else {
			hospNoteBasicJsonMapper.updateHospNoteBasicJsonByFirstGuid(jsonMapJson, "\"" + basicInfo.getVisitGuid() + "\"");
		}
	
		HospNoteVersion hospNoteVersion = new HospNoteVersion();
		hospNoteVersion.setCreateTime(new Date());
		hospNoteVersion.setFirstGuid(basicInfo.getVisitGuid());
		hospNoteVersion.setFileName(versionFileName);
		hospNoteVersion.setOperator(currentUser);
		hospNoteVersionMapper.insert(hospNoteVersion);
		
		return new Result(true, "添加成功!");
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getUnPigeonhole(MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getUnPigeonhole(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getUnPigeonholeCount(MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getUnPigeonholeCount(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPigeonholed(MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getPigeonholed(params);
	}
	
	//导出病案借阅到excel
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
    public List<Map<String,Object>> exportBorrowToExcel(MedRecordQuery params){
		return hospitalizationNoteMapper.exportBorrowToExcel(params);
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> exportUnPigeonholeToExcel(MedRecordQuery params) {
		List<Map<String, Object>> exportList=hospitalizationNoteMapper.exportUnPigeonholeToExcel(params);
		for (Map<String, Object> map : exportList) {
			for (Entry<String, Object> entrymap : map.entrySet()) {
				if(entrymap.getKey().equals("trace_date")){
					map.put("trace_date", "未归档");
				}
				
			}
		}
		return exportList;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getPigeonholedCount(MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getPigeonholedCount(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPigeonholeCheck(MedTraceQuery params) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> pigeonholedOfMedRecords = hospitalizationNoteMapper.getCollectionStatistics(params);
		List<Map<String, Object>> pigeonholeCheckkMedRecords = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> pigeonholedOfMedRecord : pigeonholedOfMedRecords) {
			Date outHospitalDate = (Date)pigeonholedOfMedRecord.get("out_hospital_date");
			Date collectionDate = (Date)pigeonholedOfMedRecord.get("trace_date");
			if(outHospitalDate.compareTo(new Date()) > 0){
				pigeonholedOfMedRecord.put("check_result", "出院日期应小于当前日期");
				
			}else if(collectionDate.compareTo(new Date()) > 0){
				pigeonholedOfMedRecord.put("check_result", "归档日期应小于当前日期");
			}
			else if(collectionDate.compareTo(outHospitalDate) < 0){
				pigeonholedOfMedRecord.put("check_result", "归档日期应不小于出院日期");
			}else{
				continue;
			}
			pigeonholeCheckkMedRecords.add(pigeonholedOfMedRecord);
		}
		return pigeonholeCheckkMedRecords;
	}

	@Autowired
	private MedicalVisitWarnDetailMapper medicalVisitWarnDetailMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int medicalVisitUrge(String[] urgeGuids, String userId, String byUrgeUserPhone) {
		// TODO Auto-generated method stub
		for (String urgeGuid : urgeGuids) {
			MedicalVisitWarnDetail warnDetail = new MedicalVisitWarnDetail();
			warnDetail.setMedicalVisitGuid(urgeGuid);
			warnDetail.setWarnUserId(userId);
			warnDetail.setByWarnUserPhone(byUrgeUserPhone);
			medicalVisitWarnDetailMapper.insert(warnDetail);
		}
		return 0;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getMedicalVisitOfUrge(
			MedVisitUrgeQuery params) {
		return hospitalizationNoteMapper.getMedicalVisitOfUnUrge(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int addQualityTrace(TreatmentTrace treatmentTrace) {
		List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_QUALITY);
		treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
		return treatmentTraceMapper.insert(treatmentTrace);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getMedRecordList(MedicalSearchQuery medicalSearchQuery) {
		
		return hospitalizationNoteMapper.getMedRecordList(medicalSearchQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getMedRecordListCount(MedicalSearchQuery medicalSearchQuery) {
		return hospitalizationNoteMapper.getMedRecordListCount(medicalSearchQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> getHospitalNoteByFirstGuid(String firstGuid) {
		
		return hospitalizationNoteMapper.getHospitalNoteByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalizationNoteOfHomePageList(
			MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getHospitalizationNoteOfHomePageList(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getHospitalizationNoteOfHomePageCount(MedRecordQuery params) {
		// TODO Auto-generated method stub
		return hospitalizationNoteMapper.getHospitalizationNoteOfHomePageCount(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public String diagAssociationCode(String diagCodes) {
		String result=null;
		List<Map<String,Object>> diagnoseTypes=  baseInfoMapper.getDiagAssociatedCode();
		if(StringUtils.isEmpty(diagCodes)){
			return result;
		}
		StringBuilder builder = new StringBuilder();
		for (Map<String, Object> map : diagnoseTypes) {
			int count=0;
			String associationCodes= (String) map.get("association_code");
			String code = (String) map.get("code");
			String name = (String) map.get("name");
			String[] codeArray= associationCodes.split(",");
			for (String  associationCode : codeArray) {
				int length = associationCode.length();
				for (String string : diagCodes.split(",")) {
					String diseaseCode =string.split("-")[1];
					int totalLength = diseaseCode.length();
					String diag="";
					if(length>totalLength){
						diag= diseaseCode.substring(0,totalLength);
					}else{
						diag= diseaseCode.substring(0,length);
					}
					if(associationCode.equals(diag)){
						builder.append(string);
						builder.append(" ");
						count++;
					}
				}
			}
			
			if(codeArray.length==count){
				builder.append("可以合并编码为:");
				builder.append(name);
				builder.append("-");
				builder.append(code);	
				result = builder.toString();
				return result;
			}
			
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getHospitalizationNotePutAway_Page(MedRecordQuery params) {
		return null;
	}

	@Override
	public int getHospitalizationNotePutAway_count(MedRecordQuery params) {
		return 0;
	}

	/**
	 * 病案检索分页 mobile
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getMedicalSearchListPage(Map<String, Object> params) {
		List<Map<String, Object>> list = hospitalizationNoteMapper.getMedicalSearchListPage(params);
		return list;
	}

	/**
	 * 病案检索总记录数 mobile
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getMedicalSearchCountPage(Map<String, Object> params) {
		int count = hospitalizationNoteMapper.getMedicalSearchCountPage(params);
		return count;
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int addSpecialMark(Integer noteId) {
		HospitalizationNote note = hospitalizationNoteMapper.selectByPrimaryKey(noteId);
		int count=0;
		if(note!=null){
			note.setSpecialIndication(2);
			count = hospitalizationNoteMapper.updateByPrimaryKeySelective(note);
		}
		return count;
	}

	/**
	 * 缺项分页统计
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getLacunaStatisticsPage(MedRecordQuery params) {
		return hospitalizationNoteMapper.getLacunaStatisticsPage(params);
	}

	/**
	 * 缺项分页统计总页数
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getLacunaStatisticsCountPage(MedRecordQuery params) {
		return hospitalizationNoteMapper.getLacunaStatisticsCountPage(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public String getLacunaCodeStrByFirstGuid(String firstGuid) {
		return hospitalizationNoteMapper.getLacunaCodeStrByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void deleteMedicalRecord(String firstGuid) {
		if(StringUtils.isEmpty(firstGuid)) {
			return;
		}
		FirstLevelMedical firstLevelMedical = firstLevelMedicalMapper.selectByPrimaryKey(firstGuid);
		String citizenGuid = firstLevelMedical.getCitizensGuid();
		HospitalizationNote  note = hospitalizationNoteMapper.selectByFirstGuid(firstGuid);
		Integer noteId = note.getId();
		hospitalizationNoteMapper.deleteByPrimaryKey(noteId);
		firstLevelMedicalMapper.deleteByPrimaryKey(firstGuid);
		citizensInfoMapper.deleteByPrimaryKey(citizenGuid);
		treatmentTraceMapper.deleteByFirstGuid(firstGuid);
		medicalResourceExaminationMapper.deleteByFirstGuid(firstGuid);
		hospNoteVersionMapper.deleteByFirstGuid(firstGuid);
		
		String filePath = myConfig.getHospitalVisitJsonPath() + StringUtil.getLocalPath(firstGuid);
		String fileName = firstGuid + ".json";
		
		//删除本地json文件
		String diskName = DiskManageUtil.getDiskNameByFileName(conf.getConfigFileStorageAllDisk(), filePath + fileName);
		
		if (StringUtils.isBlank(diskName)) {
			logger.error("本地磁盘{}里面没有找到json文件, filePath={}, visitGuid", conf.getConfigFileStorageAllDisk(), 
					filePath + fileName, firstGuid);
		} else {
			//删除本地json文件
			FileUtil.deleteFile( diskName + filePath.trim().substring(1) + fileName);
		}
		
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void updateScanFilrOriginalName(String fileHash, String preOriginalName) {
		ScanFile scanFile = scanFileMapper.selectByPrimaryKey(fileHash);
		String fileType = scanFile.getFileType();
		scanFile.setOriginalName(preOriginalName+"."+fileType);
		scanFileMapper.updateByPrimaryKeySelective(scanFile);
		
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int isHaveNopage(String firstGuid) {
		return  medicalResourceMapper.selectUnPageCount(firstGuid);
		
	}

	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getHospNoteByMrIdAndVisitNumber(String mrId, String vistiNumber) {
		return hospitalizationNoteMapper.getHospNoteByMrIdAndVisitNumber(mrId, vistiNumber);
	}

	@TargetDataSource(name="ds1")
	@Override
	public Set<Integer> batchPaginationMedrecordImage(String medicalResourceJsonStr, Integer mrPageTypeId, String firstGuid) {
		List<Map> medicalResouceList = JsonUtils.jsonToList(medicalResourceJsonStr, Map.class);
		Set<Integer> mrPageTypesSet = new HashSet<Integer>();
		
		if (null != medicalResouceList && medicalResouceList.size() > 0) {
			mrPageTypesSet.add(mrPageTypeId);
			
			for (Map mrMap : medicalResouceList) {
				String fileHash = mrMap.get("fileHash")!=null?(String)mrMap.get("fileHash"):"";
				String oldMrPageTypeId = mrMap.get("oldMrPageTypeId")!=null?(String)mrMap.get("oldMrPageTypeId"):"";
				MedicalResource medicalResource = medicalResourceMapper.selectByFirstGuidAndFileHash(fileHash, firstGuid);
				
				if (null != medicalResource) {
					medicalResource.setMrPageType(mrPageTypeId);
					medicalResource.setCreateDate(new Date());
					medicalResource.setStatus(1);
					medicalResourceMapper.updateByPrimaryKey(medicalResource);
					
					if (StringUtils.isNotBlank(oldMrPageTypeId)) {
						mrPageTypesSet.add(Integer.parseInt(oldMrPageTypeId));
					}
				}
				
			}
			
		}
		
		return mrPageTypesSet;
	}

	
	
	
	
	
}



